package com.yycx.module.bus.provider.controller;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.system.provider.service.SysBankService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 付款单控制器
 *
 * @author flyme
 * @date 2020-09-07
 */
@RestController
@RequestMapping("/bus/payment/")
public class BusPaymentController extends BaseController<BusPaymentService, BusPayment> {


    @Autowired
    private BusOrderDetailsService orderDetailsService;
    @Autowired
    private BusCustomService customService;

    @ApiOperation(value = "付款单-分页列表", notes = "付款单分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "付款单-列表", notes = "付款单列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "付款单-添加", notes = "添加付款单")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "付款单-更新", notes = "更新付款单")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "付款单-删除", notes = "删除付款单")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "付款单-详情", notes = "付款单详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "获取打印信息", notes = "获取打印信息")
    @GetMapping(value = "getPrintInfo")
    public ResultBody getPrintInfo(Long paymentId, AmountTypeEnum amountType) {
        return bizService.getPrintInfo(paymentId, amountType);
    }

    @ApiOperation(value = "获取开票税金信息", notes = "获取打印信息")
    @GetMapping(value = "getInvoicePrintInfo")
    public ResultBody getInvoicePrintInfo(Long inoviceId) {
        return bizService.getInvoicePrintInfo(inoviceId);
    }


    @ApiOperation(value = "获取认款打印信息", notes = "获取认款打印信息")
    @GetMapping(value = "getRenkuanInfo")
    public ResultBody getRenkuanInfo(String paymentId) {
        return bizService.getRenkuanInfo(paymentId);
    }


    @ApiOperation(value = "获取回款打印信息", notes = "获取回款打印信息")
    @GetMapping(value = "getHuikuanInfo")
    public ResultBody getHuikuanInfo(Long paymentId) {
        return bizService.getHuikuanInfo(paymentId);
    }


    @PostMapping(value = "initSettlePayment")
    public ResultBody initSettlePayment(@RequestParam Long orderDetailsId) {
        return bizService.initSettlePayment(orderDetailsId);
    }

    @PostMapping(value = "updatePrintTag")
    public ResultBody updatePrintTag(@RequestParam Long paymentId) {
        bizService.updatePrintTag(paymentId);
        return ResultBody.ok();
    }

    @PostMapping(value = "unBindOrder")
    public ResultBody unBindOrder(@RequestParam Long paymentId) {
        bizService.unBindOrder(paymentId);
        return ResultBody.ok("删除成功");
    }

    @ApiOperation(value = "订单已结算明细", notes = "订单已结算明细")
    @GetMapping(value = "listByOrderId")
    public ResultBody listByOrderId(@RequestParam(required = false) Long orderId, @RequestParam(required = false) Long orderDetailsId) {
        EntityMap result = new EntityMap();
        List<EntityMap> list = bizService.getByOrderId(orderId);
        List<EntityMap> settleList = orderDetailsService.getNoSettleList(orderDetailsId);
        result.put("paymentList", list);
        result.put("settleList", settleList);
        BusOrderDetails orderDetails = orderDetailsService.getById(orderDetailsId);
        if (FlymeUtils.isNotEmpty(orderDetails)) {
            Long customId = orderDetails.getCustomId();
            BusCustom custom = customService.getById(customId);
            Boolean customType = custom.getSettleType();
            if (FlymeUtils.isNotEmpty(customType) && customType) {
                result.put("settleType", 2);
            } else {
                result.put("settleType", 1);
            }
        }
        return ResultBody.ok(result);
    }

    @PostMapping(value = "exportInvoiceDetails")
    public void exportSettleDetails(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "开票税金单汇总", "开票税金单汇总", "exportInvoiceListHandler");
    }

    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "资金支付单", "资金支付单汇总", "paymentFinishPageListHandler");
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "hyExport")
    public void hyExport(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        String companyName = OpenHelper.getAttr("companyName", String.class);
        String fileName = MapUtil.getStr(params, "fileName");
        bizService.export(params, request, response,companyName+"-"+ fileName, fileName, handlerName);
    }





    @ApiOperation(value = "获取资金支付单打印信息(非两票)", notes = "获取资金支付单打印信息(非两票)")
    @GetMapping(value = "getHyPrintInfo")
    public ResultBody getHyPrintInfo(Long paymentId, AmountTypeEnum amountType) {
        return bizService.getHyPrintInfo(paymentId);
    }





}
